package com.banban.user.service.impl;

import com.banban.user.domain.ConversationLog;
import com.banban.user.domain.SessionInfo;
import com.banban.user.domain.SysUser;
import com.banban.user.dto.SessionInfoSelectDTO;
import com.banban.user.mapper.ConversationLogMapper;
import com.banban.user.mapper.SessionInfoMapper;
import com.banban.user.mapper.SysUserMapper;
import com.banban.user.service.SessionInfoService;
import com.banban.user.vo.ConversationSelectVO;
import com.banban.user.vo.SessionInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SessionInfoServiceImpl extends ServiceImpl<SessionInfoMapper, SessionInfo> implements SessionInfoService {

    @Resource
    private SessionInfoMapper sessionInfoMapper;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private ConversationLogMapper conversationLogMapper;


    @Override
    public IPage<SessionInfoVO> selectSessionInfoPage(SessionInfoSelectDTO dto) {
        String phone = dto.getPhone();
        String realName = dto.getRealName();
        String nickName = dto.getNickName();
        String startTime = dto.getStartTime();
        String endTime = dto.getEndTime();
        if (phone != null || realName != null || nickName != null){
            List<SysUser> users =  userMapper.selectUser(dto);
            if (users != null && !users.isEmpty()){
                List<String> userIds = new ArrayList<>();
                for (SysUser user : users) {
                    userIds.add(user.getId());
                }
                QueryWrapper<SessionInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("user_id",userIds);
                if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
                    queryWrapper.between("create_time",startTime,endTime);
                }
                queryWrapper.orderByDesc("id");
                List<SysUser> sysUsers = userMapper.selectBatchIds(userIds);
                Page<SessionInfo> page = this.page(dto.getQueryPage(), queryWrapper);
                return page.convert(item -> {
                    String userId = item.getUserId();
                    SessionInfoVO sessionInfoVO = new SessionInfoVO();
                    sessionInfoVO.setSessionId(item.getSessionId());
                    sessionInfoVO.setId(item.getId());
                    for (SysUser sysUser : sysUsers) {
                        if (userId.equals(sysUser.getId())){
                            sessionInfoVO.setNickName(sysUser.getNickName());
                            sessionInfoVO.setRealName(sysUser.getRealName());
                            sessionInfoVO.setPhone(sysUser.getPhone());
                            sessionInfoVO.setCreateTime(item.getCreateTime());
                        }
                    }
                    return sessionInfoVO;
                });
            }else {
                return new Page<>(dto.getPage(),dto.getLimit());
            }
        }else {
            QueryWrapper<SessionInfo> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
                queryWrapper.between("create_time",startTime,endTime);
            }
            queryWrapper.orderByDesc("id");
            Page<SessionInfo> page = this.page(dto.getQueryPage(), queryWrapper);
            if (page != null && page.getRecords() != null && !page.getRecords().isEmpty()){
                List<SessionInfo> records = page.getRecords();
                List<String> userIds = new ArrayList<>();
                for (SessionInfo record : records) {
                    userIds.add(record.getUserId());
                }
                List<SysUser> sysUsers = userMapper.selectBatchIds(userIds);
                return page.convert(item -> {
                    String userId = item.getUserId();
                    SessionInfoVO sessionInfoVO = new SessionInfoVO();
                    sessionInfoVO.setSessionId(item.getSessionId());
                    for (SysUser sysUser : sysUsers) {
                        if (userId.equals(sysUser.getId())){
                            sessionInfoVO.setNickName(sysUser.getNickName());
                            sessionInfoVO.setRealName(sysUser.getRealName());
                            sessionInfoVO.setPhone(sysUser.getPhone());
                            sessionInfoVO.setCreateTime(item.getCreateTime());
                        }
                    }
                    return sessionInfoVO;
                });
            }else {
                return new Page<>(dto.getPage(),dto.getLimit());
            }
        }
    }

    @Override
    public List<ConversationSelectVO> selectConversationBySessionId(String sessionId) {
        QueryWrapper<ConversationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("session_id",sessionId);
        List<ConversationLog> conversationLogs = conversationLogMapper.selectList(queryWrapper);
        if (conversationLogs != null && !conversationLogs.isEmpty()){
            List<ConversationSelectVO> vos = new ArrayList<>();
            for (ConversationLog conversationLog : conversationLogs) {
                ConversationSelectVO vo = new ConversationSelectVO();
                BeanUtils.copyProperties(conversationLog,vo);
                vos.add(vo);
            }
            return vos;
        }
        return Collections.emptyList();
    }
}
